package com.stedu.controller;

import cn.hutool.core.lang.Pid;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stedu.bean.Repair;
import com.stedu.bean.RespBean;
import com.stedu.exception.SteduException;
import com.stedu.service.RepairPersonnelService;
import com.stedu.service.RepairService;
import com.stedu.utils.JwtUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/admin/repair")
public class RepairController {
    @Autowired
    private RepairPersonnelService repairPersonnelService;
    @Autowired
    private RepairService repairService;
    //调用连接池里的数据
    @Value("${pageSize}")
    private Integer pageSize;

    //获取所有用户信息
    @GetMapping("/allRepair/{rid}")
    public RespBean allRepair(@PathVariable("rid") Integer rid) throws SteduException {
       List<Map<String,Object>>repairMapList = repairPersonnelService.selectByPname(null)
                .stream().map(p->{
                    Map<String, Object> repairMap = new HashMap<>();
                    repairMap.put("key", p.getPid());
                    repairMap.put("label",p.getDepartment().getDname()+"-"+p.getPname());

                    return repairMap;
        }).collect(Collectors.toList());

        List<Integer> pids = repairService.selectPidByRid(rid);
        Map<String, Object> map = new HashMap<>();
        map.put("allRepairpersonnel", repairMapList);
        map.put("selectedPids",pids);
        return RespBean.ok("",map);
    }
    @GetMapping
    public RespBean selectByPage(Integer pageNum, String oname) throws SteduException {
        if(pageNum == null) {
            pageNum = 1;
        }

        PageHelper.startPage(pageNum, pageSize);
        List<Repair> repairList = repairService.selectByOname(oname);
        PageInfo<Repair> pageInfo = new PageInfo<>(repairList);
        return RespBean.ok("查询成功", pageInfo);
    }
    @GetMapping("/selectByPageByOid")
    public RespBean selectByPageByOid(Integer pageNum, Integer oid) throws SteduException {
        if(pageNum == null) {
            pageNum = 1;
        }

        PageHelper.startPage(pageNum, pageSize);
        List<Repair> repairList = repairService.selectByOid(oid);
        PageInfo<Repair> pageInfo = new PageInfo<>(repairList);
        return RespBean.ok("查询成功", pageInfo);
    }
    @GetMapping("/selectByPageByPid")
    public RespBean selectByPageByPid(Integer pageNum, Integer pid) throws SteduException {
        if(pageNum == null) {
            pageNum = 1;
        }

        PageHelper.startPage(pageNum, pageSize);
        List<Repair> repairList = repairService.selectByPid(pid);
        PageInfo<Repair> pageInfo = new PageInfo<>(repairList);
        return RespBean.ok("查询成功", pageInfo);
    }
    @PostMapping
    public RespBean insert(@RequestBody Repair repair,@RequestHeader("token") String token) throws SteduException {
        //添加
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        if(!map.get("root").equals(1)){
        Integer oid = (Integer)map.get("oid");
        repair.setOid(oid);
        }
        if (repairService.insert(repair)) {
            return RespBean.ok("添加成功~~~");
        } else {
            return RespBean.error("添加失败，请重新添加");
        }
    }
    @DeleteMapping("/{rid}")
    public RespBean delete(@PathVariable("rid") Integer rid) throws SteduException {
        if (repairService.delete(rid)) {
            return RespBean.ok("报修已完成~~~");
        } else {
            return RespBean.error("更改失败，请重新更改");
        }
    }

    @PutMapping
    public RespBean update(@RequestBody Repair repair) throws SteduException {
        if (repairService.update(repair)) {
            return RespBean.ok("修改成功~~~");
        } else {
            return RespBean.error("修改失败，请重新修改");
        }
    }

    @GetMapping("/select/{rid}")
    public RespBean selectByRid(@PathVariable("rid") Integer rid) {
        Repair r = repairService.selectByRid(rid);
        return RespBean.ok("", r);
    }
    @PostMapping("/ridAndPid")
    public RespBean insertRidAndPid(@RequestBody Map<String,Object> map){
        Integer rid = (Integer) map.get("rid");
        List<Integer> pidsList= (List<Integer>)map.get("pids");
        Integer[] pids = new Integer[pidsList.size()];
        for(int i = 0; i < pidsList.size(); i++){
            Integer pid = pidsList.get(i);
            pids[i] = new Integer(pid);
        }
        repairService.insertRidAndPid(rid, pids);
        return RespBean.ok("分配员工成功");
    }



}
